package tests.mapeditor;

import java.io.File;

import tests.mapeditor.moks.MockMapSerializer;

import harima.agameditor.common.control.AwesomeEditorEnviroment;
import harima.agameditor.common.ui.GenericTreeNode;
import harima.agameditor.mapeditor.control.MapEditorServices;
import junit.framework.TestCase;

public class MapEditorServicesFixture extends TestCase{

	MapEditorServices services;
	
	@Override
	protected void tearDown() throws Exception {
		AwesomeEditorEnviroment.clearEnvyroment();
	}
	
	public void test_ReturnsRootNode_GetMapHyerachyRequested_Always() throws Exception {
		services = new MapEditorServices();
		GenericTreeNode rootNode = services.getMapHierachy();
		assertNotNull(rootNode);
		assertEquals(AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.MAPS_PATH), rootNode.getValue());
		assertEquals(AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.MAPS_PATH), rootNode.getValue());
	}
	
	public void test_SearchesFilesInRootDirectory_GetMapHierachyRequested_Always() throws Exception {
		services = new MapEditorServices();
		String pathSeparator = AwesomeEditorEnviroment.pathSeparator;
		AwesomeEditorEnviroment.setRootPath(pathSeparator+"home"+pathSeparator+"harima");
		MockMapSerializer serializer = new MockMapSerializer();
		services.setMapSerializer(serializer);
		services.getMapHierachy();
		assertEquals(1, serializer.CCFindFiles);

		assertEquals(AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.MAPS_PATH),
					serializer.LastFindFiles.getPath());
	}
	
	public void test_AddsNodesToRootNode_AddNodesRequested_Always() throws Exception {
		MockMapSerializer serializer = new MockMapSerializer();
		services = new MapEditorServices();
		services.setMapSerializer(serializer);
		
		File[] files = new File[3];
		for(int i=0; i<files.length; i++)
		{
			files[i] = new File("myFile"+i);
		}
		
		GenericTreeNode rootNode = new GenericTreeNode("", "");
		serializer.NextFiles = files;
		
		services.addMapsToDirectory("", rootNode);
		
		assertEquals(3, rootNode.getChildCount());
		
	}
	
	public void test_AddsNodesWithNameAndPath_AddNodesRequested_Always() throws Exception {
		File myFile = new File("myFile.txt");
		MockMapSerializer serializer = new MockMapSerializer();
		serializer.NextFiles = new File[]{myFile};
		services = new MapEditorServices();
		services.setMapSerializer(serializer);
		
		GenericTreeNode rootNode = new GenericTreeNode("", "");
		
		services.addMapsToDirectory("path",rootNode);
		
		GenericTreeNode leafNode = (GenericTreeNode)rootNode.getChildAt(0);
		assertEquals(myFile.getName(), leafNode.getName());
		assertEquals(myFile.getAbsolutePath(), leafNode.getValue());
	}
	
	
}
